/**
 * SpringMVC restful 风格
 * 用户控制器，控制关于用户的一些业务逻辑操作
 * @author lq
 * @date 2014-08-08
 */
package com.chnbst.bstyun.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.model.pojo.User;
import com.chnbst.bstyun.model.vo.UserVo;
import com.chnbst.bstyun.service.UserService;

/**
 * 控制器注解 请求映射注解
 * controller 代表类是控制器
 * restcontroller 代表类是控制器， 所有的方法 都是ajax方法
 * @author lq
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
	private String path = "mall/";
	/**
	 * 注入 业务逻辑层
	 */
	@Autowired
	private UserService userService;
	
	/**
	 * http://xxx.com/user
	 * get请求 打开默认所有用户列表
	 */
	@RequestMapping(method=RequestMethod.GET)
	public String index() throws Exception{
		return path + "user/list";
		
	}
	
	/**
	 * http://xxx.com/user/new
	 * get请求 打开添加用户页面
	 */
	@RequestMapping(value="/new",method=RequestMethod.GET)
	public String _new()  throws Exception{
		return "user/new";
	}
	
	/**
	 * http://xxx.com/user/userId
	 * get请求 打开编辑用户页面
	 */
	@RequestMapping(value="/{userId}/edit",method=RequestMethod.GET)
	public ModelAndView edit(@PathVariable Integer userId)  throws Exception{
		
		UserVo userVo = userService.selectUserByUserId(userId);
		return new ModelAndView("user/edit","UserVo",userVo);
	}
	
	/**
	 * get请求 显示单个用户详细页面
	 */
	@RequestMapping(value="/{userId}",method=RequestMethod.GET)
	public ModelAndView show(@PathVariable Integer userId)  throws Exception{
		UserVo userVo = userService.selectUserByUserId(userId);
		return new ModelAndView("user/show","UserVo",userVo);
	}
	
	/**
	 * post请求 ajax 执行添加用户
	 * @param user
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST)
	@ResponseBody
	public Object saveAjax(User user)  throws Exception{
		return userService.insertUser(user);
	}
	
	/**
	 * post请求 执行添加用户
	 * @param user
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST)
	public ModelAndView save(User user)  throws Exception{
		Object object =  userService.insertUser(user);
		
		return new ModelAndView("user/list","primarykey",object);
	}
	
	/**
	 * put请求 ajax 执行更新用户
	 * @param user
	 * @return
	 */
	@RequestMapping(method=RequestMethod.PUT)
	@ResponseBody
	public boolean updateAjax(User user)  throws Exception{
		return userService.updateUser(user);
	}
	
	/**
	 * put请求 执行更新用户
	 * @param user
	 * @return
	 */
	@RequestMapping(method=RequestMethod.PUT)
	public ModelAndView update(User user)  throws Exception{
		userService.updateUser(user);

		return new ModelAndView("user/list");
	}
	
	/**
	 * delete请求 ajax 执行删除用户
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/{userId}",method=RequestMethod.DELETE)
	@ResponseBody
	public boolean deleteAjax(@PathVariable Integer userId)  throws Exception{
		
		return userService.deleteUser(userId);
	}
	
	/**
	 * delete请求  执行删除用户
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/{userId}",method=RequestMethod.DELETE)
	public ModelAndView delete(@PathVariable Integer userId) throws Exception{
		
		return new ModelAndView("user/list");
	}
}
